package edu.its.dreamteam.web.pages.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.DraftPlayer;
import edu.its.dreamteam.daf.beans.SellPlayer;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.dao.IDraftPlayerDao;
import edu.its.dreamteam.daf.dao.ISellPlayerDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.web.base.SecureBasePage;

public class SellPlayerPage extends SecureBasePage{
	@Inject
	private ITeamDao teamDao;	
	@Inject
	private IDraftPlayerDao draftPlayerDao;
	@Inject
	private ISellPlayerDao sellPlayerDao;
	
	private List<DraftPlayer> draftPlayerList;
	private List<SellPlayer> sellPlayerList;
	private DraftPlayer draftPlayer;
	private SellPlayer sellPlayer;
	private Team team;
	
	@Retain
	private BeanModel draftPlayerModel;
	@Retain
	private BeanModel sellPlayerModel;
	@Inject
	private BeanModelSource beanModelSource;
    @Inject
    private ComponentResources resources;
    
	public DraftPlayer getDraftPlayer() {
		return draftPlayer;
	}

	public void setDraftPlayer(DraftPlayer draftPlayer) {
		this.draftPlayer = draftPlayer;
	}
	
	public SellPlayer getSellPlayer() {
		return sellPlayer;
	}

	public void setSellPlayer(SellPlayer sellPlayer) {
		this.sellPlayer = sellPlayer;
	}

	public ITeamDao getTeamDao() {
		return teamDao;
	}
	public IDraftPlayerDao getDraftPlayerDao() {
		return draftPlayerDao;
	}
		
	public ISellPlayerDao getSellPlayerDao() {
		return sellPlayerDao;
	}

	public Team getTeam() {
		if(team==null){
			team = (Team) getTeamDao().queryForBaseBean("\"username\" ='"+getVisit().getUsername()+"' ");
		}
		return team;
	}
	
	public List<DraftPlayer> getDraftPlayerList() {
		if(draftPlayerList==null){
			draftPlayerList = new ArrayList<DraftPlayer>();
			draftPlayerList = getDraftPlayerDao().queryForList("\"id_team\" ='"+getTeam().getIdTeam()+"' AND \"status_draft\"='1' AND \"sell\"='0' " , "");
		}
		return draftPlayerList;
	}
	
	public List<SellPlayer> getSellPlayerList() {
		if(sellPlayerList==null){
			sellPlayerList = new ArrayList<SellPlayer>();
			sellPlayerList = getSellPlayerDao().getSellPlayerByTeam(getTeam().getIdTeam(),getTeam().getIdRoom());
		}
		return sellPlayerList;
	}
	
	
	public BeanModel getDraftPlayerModel() {
		return draftPlayerModel;
	}
	
	

	public BeanModel getSellPlayerModel() {
		return sellPlayerModel;
	}

	void pageLoaded(){
    	draftPlayerModel = beanModelSource.create(DraftPlayer.class, false, resources);    	
    	draftPlayerModel.remove("idDraft","statusDraft","idTeam","dateDraft");
    	draftPlayerModel.get("idPlayer").label("Player");
    	draftPlayerModel.add("club",null).label("Club");
    	draftPlayerModel.add("position",null).label("Position");
    	draftPlayerModel.get("bid").label("Current Price");
    	draftPlayerModel.add("actions", null).label("Sell");
    	draftPlayerModel.reorder("idPlayer","club","position");
    	
    	sellPlayerModel = beanModelSource.create(SellPlayer.class, false, resources);    
    	sellPlayerModel.remove("idDraftPlayer","idSell");
    	sellPlayerModel.add("player",null).label("Player");
    	sellPlayerModel.add("club",null).label("Club");
    	sellPlayerModel.add("position",null).label("Position");
    	sellPlayerModel.add("bid",null).label("Price");
    	sellPlayerModel.add("actions", null).label("Sell");
    	sellPlayerModel.reorder("player","club","position");
    
	}
	
	void onSuccessFromDraftPlayerForm(String idDraft){
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		draftPlayerManager.setSell("1");
		
		SellPlayer sellPlayerManager = new SellPlayer();
		sellPlayerManager.setIdDraftPlayer(idDraft);
		sellPlayerManager.setDateSell(new Date());
		sellPlayerManager.setStatus("0");
		getSellPlayerDao().insert(sellPlayerManager);
		getDraftPlayerDao().update(draftPlayerManager);
		
	}
	void onSuccessFromSellPlayerForm(String idSell){
		SellPlayer sellPlayerManager = (SellPlayer)getSellPlayerDao().findByPrimaryKey(idSell);
		System.out.println(sellPlayerManager.getIdDraftPlayer());
		DraftPlayer draftPlayerManager = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(sellPlayerManager.getIdDraftPlayer());
		draftPlayerManager.setSell("0");
		getDraftPlayerDao().update(draftPlayerManager);
		getSellPlayerDao().delete(sellPlayerManager);
	
	}
	
}
